package com.weishui.downloading.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.Proxy.Type;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTextField;

import com.weishui.downloading.app.DownLaodWindow;
import com.weishui.downloading.app.JIpAddressField;
import com.weishui.downloading.download.DwnLoadOperator;
import com.weishui.downloading.util.Configuration;

public class ButtonActionListener implements ActionListener {

	static Configuration configuration = new Configuration();
	
	private JDialog jDialog = null;
	private JTextField jTextField = null;

	public ButtonActionListener(JDialog dialog, JTextField jTextField) {
		this.jDialog = dialog;
		this.jTextField = jTextField;
	}
	
	private JTextField textFieldDir;
	private JTextField textFieldSourceUrl;
	private JTextField threadCount;
	private JIpAddressField proxyIp;
	private JTextField proxyPort;
	public ButtonActionListener(JTextField textFieldDir,JTextField textFieldSourceUrl,JTextField threadCount,JIpAddressField proxyIp,JTextField proxyPort) {
		this.textFieldDir = textFieldDir;
		this.textFieldSourceUrl = textFieldSourceUrl;
		this.threadCount = threadCount;
		this.proxyIp = proxyIp;
		this.proxyPort = proxyPort;
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if("selectUrl".equals(event.getActionCommand())){
			JFileChooser chooser = new JFileChooser();
			chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			chooser.setDialogTitle("选择要下载的源文件");
			chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
			int ret = chooser.showOpenDialog(jDialog);
			if (ret == JFileChooser.APPROVE_OPTION) {
				File chooseFile = chooser.getSelectedFile();
				String CurrentPath = chooseFile.getAbsolutePath();
				jTextField.setText(CurrentPath);
			}
		}else if("selectDir".equals(event.getActionCommand())){
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File("c:\\"));
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.setDialogTitle("选择下载目录");
			int ret = chooser.showOpenDialog(jDialog);
			if (ret == JFileChooser.APPROVE_OPTION) {
				File chooseFile = chooser.getSelectedFile();
				String CurrentPath = chooseFile.getAbsolutePath();
				jTextField.setText(CurrentPath);
			}
		}else if("startdownLoad".equals(event.getActionCommand())){
			String dir = textFieldDir.getText();
			String urlPath = textFieldSourceUrl.getText();
			String count = threadCount.getText();
			String ip = proxyIp.getText();
			String port = proxyPort.getText();
			InetSocketAddress socketAddress = null;
			Proxy proxy = null;
			URL url = null;
			if(verifyTextInfo(dir)){
				DownLaodWindow.addErrorLog("请选择下载文件的目录!");
				return;
			}else if(verifyTextInfo(urlPath)){
				DownLaodWindow.addErrorLog("请输入下载文件的URL!");
				return;
			}else if(verifyTextInfo(count)){
				DownLaodWindow.addErrorLog("请输入下载文件的线程数!");
				return;
			}else if(DownLaodWindow.downLoadWithProxy){
				if(verifyTextInfo(ip)){
					DownLaodWindow.addErrorLog("请输入代理服务器的IP地址!");
					return;
				}else if(verifyTextInfo(port)){
					DownLaodWindow.addErrorLog("请输入代理服务器的端口PORT!");
					return;
				}
				try {
					socketAddress = new InetSocketAddress(InetAddress.getByName(ip),Integer.parseInt(port));
					proxy=new Proxy(Type.HTTP,socketAddress); 
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
			//开始下载
			try {
				url = new URL(urlPath);
				DwnLoadOperator dwnLoadOperator = new DwnLoadOperator(url,dir + "\\",Integer.parseInt(count),proxy);
				dwnLoadOperator.downLoad();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//下载完成后，保存配置信息
			configuration.setValue("downloadDir", dir);
			configuration.setValue("downloadUrl", urlPath);
			configuration.setValue("downloadCount", count);
			if(DownLaodWindow.downLoadWithProxy){
				configuration.setValue("useProxy", "1");
				configuration.setValue("proxyIp", ip);
				configuration.setValue("proxyPort", port);
			}else{
				configuration.setValue("useProxy", "0");
			}
		}
	}
	
	private static boolean verifyTextInfo(String text){
		if("".equals(text) || text == null || "0.0.0.0".equals(text)){
			return true;
		}
		return false;
	}
}
